package oop.ex2.expression;

import oop.ex2.exceptions.EmptyExpression;
import oop.ex2.exceptions.ExpectedAtomicExpression;
import oop.ex2.exceptions.ExpectedBinaryOperation;
import oop.ex2.exceptions.TerminatingStringNotFound;
import oop.ex2.exceptions.TokenNotRecognized;
import oop.ex2.tokenizer.Tokenizer;

/**
 * This is the base class for all nodes that represent a unary operation.
 * A unary operation has only one child which it operates on.
 * @author daniel_i_l
 *
 */
public abstract class UnaryOpNode extends ExpressionNode {
	private final static NodeType NODE_TYPE = NodeType.ATOM;

	/**
	 * Construct a unary operator node using the matched string.
	 * @param matchedString The string that was matched that caused this node to be generated.
	 */
	public UnaryOpNode(String matchedString) {
		super(matchedString);
		_nodeType = NODE_TYPE;
	}

	@Override
	public void generateChildren(Tokenizer tokenizer)
			throws TokenNotRecognized, EmptyExpression,
			ExpectedAtomicExpression, ExpectedBinaryOperation, TerminatingStringNotFound {

		//For a unary node, we just advance one token and build it
		if (tokenizer.isEmpty())
			throw new EmptyExpression("The unary op input cannot be empty");

		tokenizer.advance();
		ExpressionNode currentNode = tokenizer.getExpressionNode();
		addChild(currentNode);

		currentNode.generateChildren(tokenizer);
	}
}
